home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / gnodes.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  21KB  |  837 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. /* gnodes.c - translation of gnodes.stl */
  10.  
  11. #define GEN
  12.  
  13. #include "hdr.h"
  14. #include "vars.h"
  15. #include "gvars.h"
  16. #include "chapp.h"
  17. #include "gmiscp.h"
  18. #include "dbxp.h"
  19. #include "setp.h"
  20. #include "miscp.h"
  21. #include "smiscp.h"
  22. #include "chapp.h"
  23. #include "gutilp.h"
  24. #include "gnodesp.h"
  25.  
  26. /*
  27.  * Tree construction procedures
  28.  *--------------------
  29.  * 2. Lexical elements
  30.  */
  31.  
  32. Node new_number_node(int value)                                /*;new_number_node*/
  33. {
  34.     /* constructs an number node, used to hold small integer values used for
  35.      * attributes and return statement depth.
  36.      */
  37.  
  38.     Node    node;
  39.  
  40.     node        = node_new(as_number);
  41.     N_VAL (node) = (char *) value;
  42.     return node;
  43. }
  44.  
  45. Node new_instance_node(Tuple value)                        /*;new_instance_node*/
  46. {
  47.     /* constructs an instance node, used to hold tups used for instantiations */
  48.     Node    node;
  49.  
  50.     node        = node_new(as_instance_tuple);
  51.     N_VAL (node) = (char *) value;
  52.     return node;
  53. }
  54.  
  55. void make_ivalue_node(Node node, Const value, Symbol typ)  /*;make_ivalue_node*/
  56. {
  57.     /* constructs an ivalue node */
  58.  
  59.     int nk;
  60.  
  61.     nk = N_KIND(node);
  62.     if (N_AST1_DEFINED(nk)) N_AST1(node) = (Node) 0;
  63.     if (N_AST2_DEFINED(nk)) N_AST2(node) = (Node) 0;
  64.     if (N_AST3_DEFINED(nk)) N_AST3(node) = (Node) 0;
  65.     if (N_AST4_DEFINED(nk)) N_AST4(node) = (Node) 0;
  66.     if (N_LIST_DEFINED(nk)) N_LIST(node) = (Tuple) 0;
  67.     N_KIND(node) = as_ivalue;
  68.     N_VAL (node) = (char *) value;
  69.     N_TYPE(node) = typ;
  70. }
  71.  
  72. /*
  73.  *--------------------------
  74.  * 3.2.1 Object declarations
  75.  */
  76.  
  77. #ifndef BINDER
  78. void make_single_decl_list(Node root, Node decl_node) /*;make_single_decl_list*/
  79. {
  80.     /*
  81.      * This procedure transforms a declaration with a list of names into a
  82.      * list of declarations, each with one sigle name. It is called in the
  83.      * case of declarations with side-effect.
  84.      * This procedure is ineffective if the original name list has only one
  85.      * element.
  86.      * root is the root of the tree to duplicate, while decl_node points to
  87.      * the actual (original) declaration. The latter is part of the tree, but
  88.      * not necessarily the root due to possible pre-statements.
  89.      * The declaration is supposed to be fit for the first element of the
  90.      * list, so renaming is necessary for all others.
  91.      */
  92.  
  93.     Node        id_list_node, first_id, id;
  94.     Tuple    id_list, decl_list;
  95.     Symbol    first_name, first_type, id_name;
  96.     Fortup    ft1;
  97.     Symbolmap       rename_map;
  98.  
  99.     id_list_node = N_AST1(decl_node);
  100.     id_list        = tup_copy(N_LIST(id_list_node));
  101.     /* tup_copy needed since id_list used destructively in tup_fromb below*/
  102.     if (tup_size(id_list) == 1)
  103.         return;
  104.  
  105.     first_id         = (Node) tup_fromb(id_list);
  106.     N_LIST(id_list_node) = tup_new1((char *) first_id);
  107.     first_name           = N_UNQ  (first_id);
  108.     first_type           = TYPE_OF(first_name);
  109.     decl_list            = tup_new1((char *) copy_node(root));
  110.     FORTUP(id=(Node), id_list, ft1);
  111.         id_name       = N_UNQ(id);
  112.         /* RENAME_MAP    = {[first_name, id_name],
  113.            *          [first_type, TYPE_OF(id_name)] };
  114.            */
  115.         rename_map = symbolmap_new();
  116.         symbolmap_put(rename_map, first_name, id_name);
  117.         symbolmap_put(rename_map, first_type, TYPE_OF(id_name));
  118.         node_map = nodemap_new();    /* initialize */
  119.  
  120.         decl_list = tup_with(decl_list,
  121.           (char *) instantiate_tree(root, rename_map));
  122.     ENDFORTUP(ft1);
  123.  
  124.     N_KIND(root) = as_declarations;
  125.     N_AST1(root) = (Node)0; 
  126.     N_AST2(root) = (Node)0;
  127.     N_AST3(root) = (Node)0; 
  128.     N_AST4(root) = (Node)0;
  129.     N_LIST(root) = decl_list;
  130.     N_SIDE(root) = TRUE;  /* We are called only in case of side-effect */
  131. }
  132. #endif
  133.  
  134. void make_const_node(Node node, Symbol const_name, Symbol type_name,
  135.   Node init_node)                                        /*;make_const_node*/
  136. {
  137.     Node    list_node;
  138.  
  139.     list_node         = new_node(as_list);
  140.     N_KIND(node)      = as_const_decl;
  141.     N_LIST(list_node) = tup_new1((char *) new_name_node(const_name));
  142.     N_LIST(node)      = (Tuple) 0;
  143.     N_VAL (node)      = (char *) 0;
  144.     N_AST1(node)      = list_node ;
  145.     N_AST2(node)      = new_name_node(type_name) ;
  146.     N_AST3(node)      = init_node ;
  147. }
  148.  
  149. Node new_var_node(Symbol var_name, Symbol type_name, Node init_node)
  150.                                                             /*;new_var_node*/
  151. {
  152.     Node   list_node, node;
  153.  
  154.     list_node         = new_node(as_list);
  155.     N_LIST(list_node) = tup_new1((char *) new_name_node(var_name));
  156.     node              = new_node(as_obj_decl);
  157.     N_AST1(node)      = list_node ;
  158.     N_AST2(node)      = new_name_node(type_name) ;
  159.     N_AST3(node)      = init_node ;
  160.     return node;
  161. }
  162.  
  163. /*
  164.  *---------------------------
  165.  * 3.3.2 Subtype declarations
  166.  */
  167.  
  168. Node new_subtype_decl_node(Symbol type_name)        /*;new_subtype_decl_node*/
  169. {
  170.     /*
  171.      * Creates a subtype declaration node. Only type name initialized, as
  172.      * types are fully processed from the symbol table.
  173.      */
  174.     Node    node;
  175.  
  176.     node        = new_node(as_subtype_decl);
  177.     N_AST1(node) = new_name_node(type_name);
  178.     N_AST2(node) = OPT_NODE;
  179.     N_UNQ(node) = type_name;
  180.     return node;
  181. }
  182.  
  183. /*
  184.  *----------------
  185.  * 3.8 Access type
  186.  */
  187.  
  188. Node new_null_node(Symbol r_type)                            /*;new_null_node*/
  189. {
  190.     Node    node;
  191.  
  192.     node         = new_node(as_null) ;
  193.     N_TYPE(node) = r_type ;
  194.     return node ;
  195. }
  196.  
  197. /*
  198.  *----------
  199.  * 4.1 Names
  200.  */
  201.  
  202. Node new_name_node(Symbol name)                                /*;new_name_node*/
  203. {
  204.     /* constructs an as_simple_name node. */
  205.     Node    node;
  206.  
  207.     if (name == (Symbol)0)
  208.         compiler_error("Name is omega in new_name_node");
  209.     node         = new_node(as_simple_name);
  210.     N_UNQ (node) = name;
  211.     return node;
  212. }
  213.  
  214. void make_name_node(Node node, Symbol name)                /*;make_name_node*/
  215. {
  216.     /* Transforms node into an as_simple_name node. */
  217.  
  218.     if (name == (Symbol)0)
  219.         compiler_error("Name is omega in make_name_node");
  220.     N_KIND(node) = as_simple_name;
  221.     N_AST1(node) = (Node)0; 
  222.     N_AST2(node) = (Node) 0;
  223.     N_AST3(node) = (Node)0; 
  224.     N_AST4(node) = (Node) 0;
  225.     N_LIST(node) = (Tuple)0;
  226.     N_TYPE(node) = (Symbol) 0;
  227.     N_VAL (node) = (char *) 0;
  228.     N_UNQ (node) = name;
  229. }
  230.  
  231. /*
  232.  *--------------------------
  233.  * 4.1.1 Indexed components
  234.  */
  235.  
  236. Node new_index_node(Node object_node, Tuple index_list, Symbol comp_type)
  237.                                                             /*;new_index_node*/
  238. {
  239.     /* Build an as_index node */
  240.     Node    node, list_node;
  241.  
  242.     list_node         = new_node(as_list);
  243.     N_LIST(list_node) = index_list;
  244.     node              = new_node(as_index);
  245.     N_AST1(node)      = object_node ;
  246.     N_AST2(node)      = list_node ;
  247.     N_TYPE(node)      = comp_type;
  248.     return node;
  249. }
  250.  
  251. void make_index_node(Node node, Node object_node, Tuple index_list,
  252.   Symbol comp_type)                                        /*;make_index_node */
  253. {
  254.     /* Build an as_index node */
  255.     Node    list_node;
  256.  
  257.     list_node         = new_node(as_list);
  258.     N_LIST(list_node) = index_list;
  259.     N_LIST(node)      = (Tuple) 0;
  260.     N_KIND(node)      = as_index;
  261.     N_AST1(node)      = object_node ;
  262.     N_AST2(node)      = list_node ;
  263.     N_TYPE(node)      = comp_type;
  264. }
  265.  
  266. /*
  267.  *--------------------------
  268.  * 4.1.3 Selected components
  269.  */
  270.  
  271. Node new_selector_node(Node object_node, Symbol selector) /*;new_selector_node*/
  272. {
  273.     /*
  274.      * The selector  is a declared  component name, or the  internal marker
  275.      * 'constrained' used  to represent the  corresponding attribute.  This
  276.      * name is used for all records with discriminants. Its type is BOOLEAN.
  277.      */
  278.  
  279.     Node    node, sel_node;
  280.  
  281.     node         = new_node(as_selector) ;
  282.     sel_node     = new_name_node(selector) ;
  283.     N_AST1(node) = object_node ;
  284.     N_AST2(node) = sel_node ;
  285.     N_TYPE(node) = TYPE_OF(selector) ;
  286.     return node ;
  287. }
  288.  
  289. void make_selector_node(Node node, Node object_node, Symbol selector)
  290.                                                     /*;make_selector_node*/
  291. {
  292.     /*
  293.      * The selector  is a declared  component name, or the  internal marker
  294.      * 'constrained' used  to represent the  corresponding attribute.  This
  295.      * name is used for all records with discriminants. Its type is BOOLEAN.
  296.      */
  297.  
  298.     Node    sel_node;
  299.  
  300.     sel_node     = new_name_node(selector) ;
  301.     N_KIND(node) = as_selector;
  302.     N_LIST(node) = (Tuple) 0;
  303.     N_AST1(node) = object_node ;
  304.     N_AST2(node) = sel_node ;
  305.     N_TYPE(node) = TYPE_OF(selector) ;
  306. }
  307.  
  308. Node new_discr_ref_node(Symbol d_name, Symbol type_name)    /*;new_discr_ref*/
  309. {
  310.     Node    node;
  311.  
  312.     node         = new_node(as_discr_ref) ;
  313.     N_AST1(node) = new_name_node(type_name);
  314.     N_UNQ(node) = d_name;
  315.     N_TYPE(node) = TYPE_OF(d_name);
  316.     return node ;
  317. }
  318.  
  319. void make_discr_ref_node(Node node, Symbol d_name, Symbol type_name)
  320.                                                     /*;make_discr_ref_node*/
  321. {
  322.     N_KIND(node) = as_discr_ref;
  323.     N_AST1(node) = new_name_node(type_name);
  324.     N_LIST(node) = (Tuple) 0;
  325.     N_UNQ (node) = d_name;
  326.     N_TYPE(node) = TYPE_OF(d_name);
  327. }
  328.  
  329. /*
  330.  *-----------------
  331.  * 4.1.4 Attributes
  332.  */
  333.  
  334. Node new_attribute_node(int attr, Node arg1, Node arg2, Symbol typ)
  335.                                                         /*;new_attribute_node*/
  336. {
  337.     /* Creates an attribute node. attr is the attribute's name. */
  338.  
  339.     Node    id_node, attr_node;
  340.  
  341.     id_node           = new_node(as_number);
  342.     attr_node         = new_node(as_attribute);
  343.     /* attach attribute code to parent node */
  344.     N_AST1(attr_node) = id_node ;
  345.     attribute_kind(attr_node)   = (char *) attr;
  346.     N_AST2(attr_node) = arg1 ;
  347.     N_AST3(attr_node) = arg2 ;
  348.     N_TYPE(attr_node) = typ;
  349.     return attr_node;
  350. }
  351.  
  352. /*
  353.  *----------------------------------------
  354.  * 4.5 Operators and expression evaluation
  355.  */
  356.  
  357. Node new_binop_node(Symbol oper, Node arg1, Node arg2, Symbol typ)
  358.                                                             /*;new_binop_node*/
  359. {
  360.     /* Creates a binary operator node. Oper is the operator's name */
  361.  
  362.     Node    id_node, list_node, node;
  363.     Tuple    tup;
  364.  
  365.     id_node           = new_name_node(oper);
  366.     list_node         = new_node(as_list);
  367.     tup = tup_new(2);
  368.     tup[1] = (char *) arg1;
  369.     tup[2] = (char *) arg2;
  370.     N_LIST(list_node) = tup;
  371.     node              = new_node(as_op);
  372.     N_AST1(node)      = id_node ;
  373.     N_AST2(node)      = list_node ;
  374.     N_TYPE(node)      = typ;
  375.     return node;
  376. }
  377.  
  378. /*
  379.  *--------------------
  380.  * 4.6 Type conversion
  381.  */
  382.  
  383. Node new_qual_range_node(Node expr_node, Symbol type_name)
  384.                                                         /*;new_qual_range_node*/
  385. {
  386.     /* Creates a qual_range node */
  387.  
  388.     Node    node;
  389.  
  390.     node = new_node(as_qual_range);
  391.     N_TYPE(node) = type_name;
  392.     N_AST1(node) = expr_node;
  393.     return node;
  394. }
  395.  
  396. /*
  397.  *------------------------------------------------------------
  398.  * 5.1 Simple and compound statements - Sequence of statements
  399.  */
  400.  
  401. Node new_statements_node(Tuple stmt_list)            /*;new_statements_node*/
  402. {
  403.     /* Creates an as_statements node, given a list (tuple) of statements */
  404.  
  405.     Node    stmt_node, list_node;
  406.  
  407.     stmt_node         = new_node(as_statements);
  408.     list_node         = new_node(as_list);
  409.     N_LIST(list_node) = stmt_list;
  410.     N_AST1(stmt_node) = list_node ;
  411.     N_AST2(stmt_node) = OPT_NODE ;
  412.     return stmt_node;
  413. }
  414.  
  415. void make_statements_node(Node node, Tuple stmt_list) /*;make_statements_node*/
  416. {
  417.     /* Transforms node into an as_statements node, given a list (tuple)
  418.      * of statements
  419.      */
  420.  
  421.     Node    list_node;
  422.  
  423.     list_node         = new_node(as_list);
  424.     N_LIST(list_node) = stmt_list;
  425.     N_KIND(node) = as_statements;
  426.     N_LIST(node) = (Tuple) 0;
  427.     N_VAL (node) = (char *) 0;
  428.     N_AST1(node) = list_node ;
  429.     N_AST2(node) = OPT_NODE ;
  430. }
  431.  
  432. /*
  433.  *-------------------------
  434.  * 5.2 Assignment statement
  435.  */
  436.  
  437. Node new_assign_node(Node lhs, Node rhs)                /*;new_assign_node*/
  438. {
  439.     /* Creates an assign node */
  440.  
  441.     Node    node;
  442.  
  443.     node        = new_node(as_assignment) ;
  444.     N_AST1(node) = lhs ;
  445.     N_AST2(node) = rhs  ;
  446.     return node ;
  447. }
  448.  
  449. void make_assign_node(Node node, Node lhs, Node rhs)    /*;make_assign_node*/
  450. {
  451.     /* Transforms node into an assign node */
  452.  
  453.     N_KIND(node) = as_assignment;
  454.     N_LIST(node) = (Tuple) 0;
  455.     N_VAL (node) = (char *) 0;
  456.     N_AST1(node) = lhs ;
  457.     N_AST2(node) = rhs  ;
  458. }
  459.  
  460. /*
  461.  *-----------------
  462.  * 5.3 If statement
  463.  */
  464.  
  465. Node new_simple_if_node(Node cond_node, Node then_node, Node else_node)
  466.                                                         /*;new_simple_if_node*/
  467. {
  468.     /* Creates an if node for the case without elsif parts */
  469.  
  470.     Node    node, cond_stmt_node, if_list_node;
  471.  
  472.     cond_stmt_node        = new_node(as_cond_statements) ;
  473.     N_AST1(cond_stmt_node) = cond_node;
  474.     N_AST2(cond_stmt_node) = then_node  ;
  475.     if_list_node          = new_node(as_list) ;
  476.     N_LIST(if_list_node)  = tup_new1((char *) cond_stmt_node);
  477.     node                  = new_node(as_if) ;
  478.     N_AST1(node)           = if_list_node ;
  479.     N_AST2(node)           = else_node  ;
  480.     return node;
  481. }
  482.  
  483. void make_if_node(Node node, Tuple cond_list, Node else_stmt_node)
  484.                                                             /*;make_if_node*/
  485. {
  486.     /*
  487.      * Transforms node into an if statement.
  488.      * cond_list is a list (tuple) of conditions node
  489.      * others_stmt is the node corresponding to the else part
  490.      */
  491.  
  492.     Node    list_node;
  493.  
  494.     list_node         = new_node(as_list);
  495.     N_LIST(list_node) = cond_list;
  496.  
  497.     N_KIND(node)      = as_if;
  498.     N_LIST(node)      = (Tuple) 0;
  499.     N_VAL(node)      = (char *) 0;
  500.     N_AST1(node)      = list_node ;
  501.     N_AST2(node)      = else_stmt_node ;
  502. }
  503.  
  504. Node new_cond_stmts_node(Node expr_node, Node stmts_node)
  505.                                                         /*;new_cond_stmts_node*/
  506. {
  507.     /*
  508.      * Creates a conditional statements node.
  509.      * Expr is the (boolean) expression, stat the statements
  510.      */
  511.  
  512.     Node    cond_node;
  513.  
  514.     cond_node        = new_node(as_cond_statements);
  515.     N_AST1(cond_node) = expr_node ;
  516.     N_AST2(cond_node) = stmts_node ;
  517.     return cond_node;
  518. }
  519.  
  520. /*
  521.  *-------------------
  522.  * 5.5 Loop statement
  523.  */
  524.  
  525. Node new_loop_node(Node label_node, Node iter_node, Tuple stmt_list)
  526.                                                             /*;new_loop_node*/
  527. {
  528.     /* Creates a loop node */
  529.  
  530.     Node    loop_node, stmt_node;
  531.  
  532.     loop_node         = new_node(as_loop);
  533.     stmt_node         = new_statements_node(stmt_list);
  534.     N_AST1(loop_node) = label_node ;
  535.     N_AST2(loop_node) = iter_node ;
  536.     N_AST3(loop_node) = stmt_node ;
  537.     return loop_node;
  538. }
  539.  
  540. /*
  541.  *--------------------
  542.  * 5.6 Block statement
  543.  */
  544.  
  545. Node new_block_node(Node label_node, Tuple decl_list, Tuple stmt_list,
  546.   Node exc_node)                                        /*;new_block_node*/
  547. {
  548.     /* Creates a block node */
  549.  
  550.     Node    block_node, decl_node, stmt_node;
  551.  
  552.     block_node = new_node(as_block);
  553.  
  554.     if (tup_size(decl_list) == 0) {
  555.         decl_node = OPT_NODE;
  556.     }
  557.     else {
  558.         decl_node         = new_node(as_declarations);
  559.         N_LIST(decl_node) = decl_list;
  560.     }
  561.  
  562.     stmt_node = new_statements_node(stmt_list);
  563.  
  564.     N_AST1(block_node) = label_node ;
  565.     N_AST2(block_node) = decl_node ;
  566.     N_AST3(block_node) = stmt_node ;
  567.     N_AST4(block_node) = exc_node ;
  568.     return block_node;
  569. }
  570.  
  571. /*
  572.  *---------------------------
  573.  * 6.1 Subprogram declaration
  574.  */
  575.  
  576. void make_subprog_node(Node node, Symbol type_name, Node decl_node,
  577.   Node stmt_node, Node exc_node)                        /*;make_subprog_node*/
  578. {
  579.     /* Note format of as_subprogram_tr node is different than that of 
  580.      * as_subprogram. This format is much more concise with all unnecessary
  581.      * tree structure disgarded.
  582.      */
  583.  
  584.     N_KIND(node) = as_subprogram_tr;
  585.     N_AST1(node) = stmt_node ;
  586.     N_AST2(node) = decl_node ;
  587.     N_UNQ(node) = type_name ;
  588.     N_AST4(node) = exc_node ;
  589. }
  590.  
  591. Node new_param_node(char *param_name, Symbol scope_name, Symbol type_name,
  592.   int na_mode)                                                /*;new_param_node*/
  593. {
  594.     /* This procedure is called from initobj. The actual string passed as
  595.      * the first argument is not important and is used in SETL version
  596.      * only for debugging purposes.
  597.      */
  598.  
  599.     Node    node;
  600.     Symbol    param_unam;
  601.  
  602.  
  603.     param_unam = new_unique_name(param_name) ;
  604.     node       = new_name_node(param_unam) ;
  605.  
  606.     TYPE_OF(param_unam)  = type_name;
  607.     NATURE (param_unam)  = na_mode;
  608.     SCOPE_OF(param_unam) = scope_name;
  609.     return node ;
  610. }
  611.  
  612. /*
  613.  *--------------------
  614.  * 6.4 Subprogram call
  615.  */
  616.  
  617. Node new_call_node(Symbol proc_name, Tuple arg_list, Symbol type_name)
  618.                                                             /*;new_call_node*/
  619. {
  620.     /* Creates a call node. */
  621.  
  622.     Node    list_node, call_node;
  623.  
  624.     list_node         = new_node(as_list);
  625.     N_LIST(list_node) = arg_list;
  626.     call_node         = new_node(as_call);
  627.     N_TYPE(call_node) = type_name;
  628.     N_AST1(call_node) = new_name_node(proc_name) ;
  629.     N_AST2(call_node) = list_node ;
  630.     return call_node;
  631. }
  632.  
  633. /*
  634.  *--------------------------------
  635.  * 9.2 Task types and task objects
  636.  */
  637.  
  638. Node new_create_task_node(Symbol c_type)            /*;new_create_task_node*/
  639. {
  640.     Node    node;
  641.  
  642.     node         = new_node(as_create_task) ;
  643.     N_TYPE(node) = c_type ;
  644.     N_SIDE(node) = TRUE;
  645.     return node ;
  646. }
  647.  
  648. /*
  649.  *---------------------
  650.  * 11.3 Raise statement
  651.  */
  652.  
  653. Node new_raise_node(Symbol exc_name)                    /*;new_raise_node */
  654. {
  655.     /* creates a raise node */
  656.  
  657.     Node    raise_node;
  658.  
  659.     raise_node = new_node(as_raise);
  660.     N_AST1(raise_node) = (exc_name == OPT_NAME) ? OPT_NODE :
  661.       new_name_node(exc_name);
  662.     return raise_node;
  663. }
  664.  
  665. void make_raise_node(Node node, Symbol exc_name)        /*;make_raise_node*/
  666. {
  667.     /* Transforms node into a raise statement */
  668.  
  669.     N_KIND(node) = as_raise;
  670.     N_AST1(node) = (exc_name == OPT_NAME) ? OPT_NODE :
  671.       new_name_node(exc_name);
  672.     N_LIST(node) = (Tuple) 0;
  673.     N_VAL (node) = (char *) 0;
  674. }
  675.  
  676. /*
  677.  *--------------
  678.  * Miscelleanous
  679.  */
  680.  
  681. void make_insert_node(Node node, Tuple pre_list, Node post_node)
  682.                                                         /*;make_insert_node*/
  683. {
  684.     /* Transforms node into an insert node */
  685.  
  686.     if (N_AST3_DEFINED(N_KIND(node))) { /* clear to avoid confusing with N_UNQ*/
  687.         N_AST3(node) = (Node) 0;
  688.     }
  689.     /* warn if n_ast4 and N_type may be confused */
  690.     if (N_AST4_DEFINED(N_KIND(node))) {
  691. #ifdef DEBUG
  692.         zpnod(node);
  693. #endif
  694.         chaos("make_insert_node error: ");
  695.         N_AST4(node) = (Node) 0;
  696.     }
  697.     N_KIND(node) = as_insert;
  698.     N_VAL (node) = (char *) 0;
  699.     N_AST1(node) = post_node;
  700.     N_LIST(node) = pre_list;
  701.     if (N_TYPE_DEFINED(N_KIND(post_node))) {
  702.         N_TYPE(node) = N_TYPE(post_node);
  703.     }
  704. }
  705.  
  706. void propagate_insert(Node from_node, Node to_node)        /*;propagate_insert*/
  707. {
  708.     /* From node is an insert node that is to be propagated to to_node */
  709.  
  710.     Node    post_node;
  711.     Tuple    pre_list;
  712.  
  713.     post_node = N_AST1(from_node);
  714.     pre_list    = N_LIST(from_node);
  715.     copy_attributes(post_node, from_node);
  716.     copy_attributes(to_node, post_node);
  717.     make_insert_node(to_node, pre_list, post_node);
  718. }
  719.  
  720. Node new_expanded_node(Node exp_node)                    /*;new_expanded_node*/
  721. {
  722.     /* Creates an as_expanded node */
  723.  
  724.     Node    node;
  725.  
  726.     if (exp_node == OPT_NODE)  return OPT_NODE;
  727.     node        = new_node(as_expanded);
  728.     N_AST1(node) = exp_node;
  729.     return node;
  730. }
  731.  
  732. /* Node management */
  733. Node copy_node(Node node1)                                    /*;copy_node*/
  734. {
  735.     Node    node2;
  736.  
  737.     if (node1 == OPT_NODE) {
  738.         compiler_error("Attempt to copy OPT_NODE");
  739.         return OPT_NODE;
  740.     }
  741.     node2 = node_new(N_KIND(node1));
  742.     copy_attributes(node1, node2) ;
  743.     return node2;
  744. }
  745.  
  746. void copy_attributes(Node old_node, Node target_node)        /*;copy_attributes*/
  747. {
  748.     int     nk;
  749.  
  750.     if (N_KIND(old_node) == as_opt)
  751.         compiler_error("Copying OPT kind");
  752.  
  753.     nk = N_KIND(old_node);
  754.     N_KIND(target_node) = nk;
  755.     if (N_AST1_DEFINED(nk)) N_AST1(target_node) = N_AST1(old_node);
  756.     if (N_AST2_DEFINED(nk)) N_AST2(target_node) = N_AST2(old_node);
  757.     if (N_AST3_DEFINED(nk)) N_AST3(target_node) = N_AST3(old_node);
  758.     if (N_AST4_DEFINED(nk)) N_AST4(target_node) = N_AST4(old_node);
  759.     if (N_LIST_DEFINED(nk)) N_LIST(target_node) = N_LIST(old_node);
  760.     if (N_VAL_DEFINED(nk)) N_VAL (target_node) = N_VAL (old_node);
  761.     N_UNQ (target_node) = N_UNQ (old_node);
  762.     N_TYPE(target_node) = N_TYPE(old_node);
  763.     N_SIDE(target_node) = N_SIDE(old_node);
  764. #ifdef TBSL
  765.     -- see if any other fields need becopied
  766. #endif
  767. }
  768.  
  769. Node copy_tree(Node node)                                        /*;copy_tree*/
  770. {
  771.     /* Create a full copy of the tree rooted at node, and return the new root*/
  772.  
  773.     Fortup    ft1;
  774.     Tuple    tup;
  775.     Node n, root;
  776.     int i, nk;
  777.  
  778.     if (node == (Node)0 || node == OPT_NODE)
  779.         return (node);
  780.     root = node_new(N_KIND(node));
  781.  
  782.     nk = N_KIND(node);
  783.     if (N_VAL_DEFINED(nk)) N_VAL (root) = N_VAL (node) ;
  784.     /* N_UNQ defined only if N_AST3 not defined */
  785.     if (!N_AST3_DEFINED(nk)) N_UNQ (root) = N_UNQ (node) ;
  786.     /* N_TYPE defined only if N_AST4 not defined */
  787.     if (!N_AST4_DEFINED(nk)) N_TYPE(root) = N_TYPE(node) ;
  788.     N_SIDE(root) = N_SIDE(node) ;
  789.     if (N_AST1_DEFINED(nk)) N_AST1(root) = copy_tree(N_AST1(node));
  790.     if (N_AST2_DEFINED(nk)) N_AST2(root) = copy_tree(N_AST2(node));
  791.     if (N_AST3_DEFINED(nk)) N_AST3(root) = copy_tree(N_AST3(node));
  792.     if (N_AST4_DEFINED(nk)) N_AST4(root) = copy_tree(N_AST4(node));
  793.  
  794.     if (N_LIST_DEFINED(nk) && N_LIST(node) != (Tuple)0) {
  795.         tup = tup_new(tup_size(N_LIST(node)));
  796.         FORTUPI(n=(Node), N_LIST(node), i, ft1);
  797.             tup[i] = (char *) copy_tree(n);
  798.         ENDFORTUP(ft1);
  799.         N_LIST(root) = tup ;
  800.     }
  801.     return root;
  802. }
  803.  
  804. Node new_node(int kind)                                            /*;new_node*/
  805. {
  806.     Node    node;
  807.  
  808.     node = node_new(kind);
  809.     return node;
  810. }
  811.  
  812. void delete_node(Node node)                                    /*;delete_node*/
  813. {
  814. #ifdef TRACE
  815.     if (debug_flag)
  816.         gen_trace_node("DELETE", node);
  817. #endif
  818.  
  819.     N_KIND(node) = as_deleted;
  820.     N_SIDE(node) = FALSE;
  821.     N_VAL (node) = (char *) 0;
  822.     N_LIST(node) = (Tuple) 0;
  823.     N_AST1(node) = N_AST2(node) = N_AST3(node) = N_AST4(node) = (Node) 0;
  824. }
  825.  
  826. void copy_span(Node old, Node newn)                            /*;copy_span */
  827. {
  828.     /* make this a noop in the code generator as spans field is undefined */
  829.     return;
  830. }
  831.  
  832. int is_terminal_node(short kind)                    /*;is_terminal_node */
  833. {
  834.     /* noop in generator as spans field is undefined */
  835.     return 0;
  836. }
  837.